package org.template.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.template.dao.iter.RedisDelegator;
import org.template.dao.iter.RedisDelegatorNoResult;

import redis.clients.jedis.Jedis;

@Repository
public class HashMapRedisDao extends BaseRedisDao {

	public String getString(final String hmKeyInRedis, final String field) {

		return run(new RedisDelegator<String>() {
			@Override
			public String execute(Jedis redis) {
				return redis.hget(hmKeyInRedis, field);
			}
		});
	}

	// TODO problems
	public void setParams(final String key, final String field, final String value) {
		runNoResult(new RedisDelegatorNoResult() {
			@Override
			public void execute(Jedis redis) {
				redis.hsetnx(key, field, value);
			}
		});

	}

	public List<String> batchGetString(final String key, final String... field) {

		return run(new RedisDelegator<List<String>>() {
			@Override
			public List<String> execute(Jedis redis) {
				return redis.hmget(key, field);
			}
		});

	}

	public boolean batchSetParams(final String key, final Map<String, String> params) {

		return run(new RedisDelegator<Boolean>() {
			@Override
			public Boolean execute(Jedis redis) {
				return   "OK".equalsIgnoreCase(  redis.hmset(key, params));
			}
		});

	}

	public long getSize(final String key) {

		return run(new RedisDelegator<Long>() {
			@Override
			public Long execute(Jedis redis) {
				return redis.hlen(key);
			}
		});

	}

}
